package com.vhall.component.framework.common.utils;

import com.vhall.component.framework.common.constant.CommonConstant;
import com.vhall.component.framework.common.constant.SecurityConstant;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * @author yongzheng.xu
 * @date 2021/6/8 14:27
 */
public class WebUtils {

    public static void setLoginUserId(ServletRequest request, Long userId) {
        request.setAttribute(SecurityConstant.USER_ID_HEADER, userId);
    }

    /**
     * 获得当前用户的编号，从请求中
     *
     * @param request 请求
     * @return 用户编号
     */
    public static Long getLoginUserId(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        return (Long) request.getAttribute(SecurityConstant.USER_ID_HEADER);
    }

    public static Long getLoginUserId() {
        HttpServletRequest request = getRequest();
        return getLoginUserId(request);
    }

    public static void setResult(ServletRequest request, Object result) {
        request.setAttribute(CommonConstant.COMMON_RESULT_KEY, result);
    }

    public static Object getResult(ServletRequest request) {
        return request.getAttribute(CommonConstant.COMMON_RESULT_KEY);
    }

    private static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (!(requestAttributes instanceof ServletRequestAttributes)) {
            return null;
        }
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        return servletRequestAttributes.getRequest();
    }
}
